package com.smartboard.network.ui;

import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.smartboard.network.R;
import com.smartboard.network.client.GameClient;
import com.smartboard.network.game.AbstractGame;
import com.smartboard.network.game.IPlayGame;
import com.smartboard.network.game.IRoom;

import java.util.List;

/**
 * Created by cheas on 2018/8/23.
 */

public abstract class CreateGameDialog extends BaseDialog {

    protected ProgressBar mProgressBar;

    protected CreateGameTask mCreateGameTask = null;
    protected IRoom mRoom;
    protected GameActivity mActivity;

    public CreateGameDialog(GameActivity activity, IRoom room) {
        super(activity);

        mRoom = room;
        mActivity = activity;
        setTitle(R.string.title_create_game);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initViews();

        mProgressBar = (ProgressBar)findViewById(R.id.progress);

        Button okBtn = (Button)findViewById(R.id.okBtn);
        Button cancelBtn = (Button)findViewById(R.id.cancenBtn);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCreateGameTask != null)
                    return;
                if(!checkFieldValid())
                    return;
                MyApplication.hideKeyboard(mActivity);
                mCreateGameTask = new CreateGameTask(getGameInfo(), getGameNum());
                mCreateGameTask.execute();
                showProgress(true);
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCreateGameTask != null)
                    return;
                dismiss();
            }
        });
        adjustWindowSize();
    }

    protected abstract void initViews();
    protected abstract boolean checkFieldValid();
    protected abstract Object getGameInfo();
    protected abstract int getGameNum();

    private void showProgress(boolean show) {
        if(show) {
            mProgressBar.setVisibility(View.VISIBLE);

        } else {
            mProgressBar.setVisibility(View.GONE);
        }
    }

    private class CreateGameTask extends AsyncTask<Void, Void, List<IPlayGame> > {
        Object gameInfo;
        int gameNum;

        public CreateGameTask(Object gameInfo, int gameNum) {
            this.gameInfo = gameInfo;
            this.gameNum = gameNum;
        }

        @Override
        protected List<IPlayGame> doInBackground(Void... params) {
            return GameClient.inst().createGames(mRoom, gameNum, gameInfo);
        }

        @Override
        protected void onPostExecute(List<IPlayGame> games) {
            mCreateGameTask = null;
            showProgress(false);

            switch (GameClient.inst().getErrorCode()) {
                case AbstractGame.ERROR_NONE:
                    showToast(R.string.create_success);
                    dismiss();
                    break;
                case AbstractGame.ERROR_TIMEOUT:
                    showToast(R.string.create_fail_nework);
                    break;
                default:
                    break;
            }
        }

        @Override
        protected void onCancelled() {
            mCreateGameTask = null;
            showProgress(false);
        }
    }
}
